package com.fyzk.activity.user.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.chongzhi.RechargeActivity;
import com.fyzk.activity.dialog.AppDialog;
import com.fyzk.activity.dialog.AppDialog.OnAppDialogClickListener;
import com.fyzk.activity.user.PerfectInforActivity;
import com.fyzk.activity.user.WithdrawActivity;
import com.fyzk.base.BaseActivity;
import com.fyzk.bean.AccountMoneyResponse;
import com.fyzk.bean.BankListResponse;
import com.fyzk.bean.UserInfoResponse;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.fyzk.net.http.NetWork;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 个人信息界面
 */
public class PersonalInformationActivity extends BaseActivity implements onTopTitleClickListener,
        PersonalInforBankAdapter.DeleteBankListener {

    // 用户信息
    private String leftMoney;

    private TopTitleView topView;

    private TextView center_userName;
    private TextView center_shenFen;
    private TextView center_loginName;

    private TextView center_phone;

    private TextView bottom_yue;
    private TextView bottom_tixianMoney;
    private TextView bottom_chongZhi;
    private TextView bottom_tixian;

    private ListView bank_listView;
    private PersonalInforBankAdapter adapter;

    private Button netprogressview;

    public int deleteBankposition = 0;
    //获取完信息后才可以点击按钮进行下一步（充值、提现）
    private boolean isClick = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_information);
        NetWork.netWork();
        info();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    protected void onResume() {
        super.onResume();

        requestUserInfo();
        requestLeftMoney();
        requestBankList();
    }

    // 查询账户彩金
    private void requestLeftMoney() {
        httpPostJson(CommandUtil.ACCOUNT_MONEY, Commands.ACCOUNT_MONEY, null, true);
    }

    //查询用户信息
    private void requestUserInfo() {
        httpPostJson(CommandUtil.USERINFO, Commands.USERINFO, null, false);
    }

    //查询用户认证的银行卡列表
    private void requestBankList() {
        httpPostJson(CommandUtil.BANK_CARD_MANAGEMENT, Commands.FINDUSERDRAWBIND, null, false);
    }

    //请求解除银行卡绑定
    private void requestUnBind(String bankId) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("id", bankId);
            httpPostJson(CommandUtil.BANK_DELETE, Commands.USERUNBIND, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private AccountMoneyResponse accountMoneyResponse;
    private UserInfoResponse userInfoResponse;
    private List<BankListResponse> bankListResponse;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.ACCOUNT_MONEY:
                accountMoneyResponse = JSON.parseObject(resultBean.getData(),
                        AccountMoneyResponse.class);
                bottom_yue.setText(MySharedPreferences.getInstance().getBalance());
                bottom_tixianMoney.setText("" + (accountMoneyResponse.getDrawbalance() / 100.0));
                isClick = true;
                break;
            case CommandUtil.USERINFO:
                userInfoResponse = JSON.parseObject(resultBean.getData(), UserInfoResponse.class);
                kongJianFuZhi();
                isClick = true;
                break;
            case CommandUtil.BANK_CARD_MANAGEMENT:
                netprogressview.setVisibility(View.INVISIBLE);
                bankListResponse = JSON.parseArray(resultBean.getData(), BankListResponse.class);
                LogUtil.d("查询银行卡列表成功" + bankListResponse.size());
                adapter.setList(bankListResponse);
                break;
            case CommandUtil.BANK_DELETE:
                AppToast.getToast().show("已删除");
                bankListResponse.remove(deleteBankposition);
                adapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        cancelProgressDialog();
        switch (requestCode) {
            case CommandUtil.BANK_CARD_MANAGEMENT:
                if (errorCode.equals("900003")) {
                    netprogressview.setVisibility(View.VISIBLE);
                } else {
                    netprogressview.setVisibility(View.VISIBLE);
                    netprogressview.setText("联网失败");
                }
                break;
        }
    }

    //控件初始化
    private void info() {
        topView = (TopTitleView) findViewById(R.id.information_topview);
        topView.setRightVisible(View.INVISIBLE);
        topView.setCenterText(getString(R.string.user_zhu_menu_geren));
        topView.setOnTitleClickListener(this);

        center_userName = (TextView) findViewById(R.id.information_username);
        center_shenFen = (TextView) findViewById(R.id.information_shenfen);
        center_loginName = (TextView) findViewById(R.id.information_loginname);
        center_phone = (TextView) findViewById(R.id.information_center_et_cell);

        bottom_yue = (TextView) findViewById(R.id.information_yue);
        bottom_tixianMoney = (TextView) findViewById(R.id.information_tixian);

        bottom_chongZhi = (TextView) findViewById(R.id.information_btn_chongzhi);
        bottom_tixian = (TextView) findViewById(R.id.information_btn_tixian);
        bank_listView = (ListView) findViewById(R.id.information_bank_listview);
        adapter = new PersonalInforBankAdapter(this, bankListResponse, this);
        bank_listView.setAdapter(adapter);
        netprogressview = (Button) findViewById(R.id.netprogressview);
        //底部
        bottom_chongZhi.setOnClickListener(this);
        bottom_tixian.setOnClickListener(this);

    }

    //控件赋值
    private void kongJianFuZhi() {
        if (MySharedPreferences.getInstance().getAlias().equals("")) {
            center_loginName.setText(MySharedPreferences.getInstance().getUserName());
        } else {
            center_loginName.setText(MySharedPreferences.getInstance().getAlias());
        }
        if (userInfoResponse.getRealName().equals("")) {
            center_userName.setText("未绑定");
        } else {
            center_userName.setText(PublicMethod.tv_yinCang(userInfoResponse.getRealName
                    (), 1, 0));
        }

        if (userInfoResponse.getIdcard().equals("")) {
            center_shenFen.setText("未绑定");
        } else {
            center_shenFen.setText(PublicMethod.tv_yinCang(userInfoResponse.getIdcard(),
                    2, 2));
        }
        if (userInfoResponse.getPhone().equals("")) {
            center_phone.setText("未绑定");
        } else {
            center_phone.setText(PublicMethod.tv_yinCang(userInfoResponse.getPhone(), 0,
                    3));
        }

    }

    /**
     * 是否可进行下一步操作
     *
     * @return
     */
    private boolean isNext() {
        if (!isClick) {
            AppToast.getToast().show("正在获取数据，请稍候...");
            return false;
        }

        if (accountMoneyResponse == null || userInfoResponse == null) {
            AppToast.getToast().show("网络异常");
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View arg0) {
        if (isNext()) {
            Intent intent;
            switch (arg0.getId()) {
                case R.id.information_btn_chongzhi:
                    intent = new Intent(PersonalInformationActivity.this, RechargeActivity.class);
                    PersonalInformationActivity.this.startActivity(intent);
                    break;
                case R.id.information_btn_tixian:
                    //判断用户信息是否已经完善(根据真实姓名是否为空判断)
                    if (!userInfoResponse.getRealName().equals("") && !userInfoResponse.getIdcard
                            ().equals("")) {

                        intent = new Intent(PersonalInformationActivity.this,
                                WithdrawActivity.class);
                        intent.putExtra(IntentParamUtils.REALNAME, userInfoResponse.getRealName());
                        startActivity(intent);
                    } else {
                        AppDialog.getDialog().showPerfectInforDialog(PersonalInformationActivity
                                .this, new OnAppDialogClickListener() {

                            @Override
                            public void onSureClick() {
                                // TODO Auto-generated method stub
                                Intent intent2 = new Intent(PersonalInformationActivity.this,
                                        PerfectInforActivity.class);
                                startActivity(intent2);
                            }

                            @Override
                            public void onCancleClick() {
                                // TODO Auto-generated method stub
//								AppToast.getToast().show("取消。。 ");
                            }
                        });
                    }

                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        onBackPressed();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }

    @Override
    public void deleteBank(final int postion) {
        deleteBankposition = postion;
        AppDialog.getDialog().showDeleteBankDialog(PersonalInformationActivity.this, new
                OnAppDialogClickListener() {

                    @Override
                    public void onSureClick() {
                        // TODO Auto-generated method stub
                        requestUnBind(bankListResponse.get(postion).getId());
                    }

                    @Override
                    public void onCancleClick() {
                        // TODO Auto-generated method stub
                    }
                });
    }
}
